﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HA.ModFlow.Domain.DomainLayer;
using HAUtilWpf;
namespace HA.ModFlow.Controls.Windows.SourceSinks
{
    /// <summary>
    /// winUzfPage.xaml 的交互逻辑
    /// </summary>
    public partial class winUzfPage : Window
    {
        public winUzfPage(ModFlowData modflow)
        {
            InitializeComponent();
            this._uzfCollection = new PCUzfCollection(modflow);
            this.cbiFinf.Content = "FINF.入渗速率(" + Domain.DomainLayer.UnitConverter.GetLength(modflow.ModelOption.Units.Length) + "/" + Domain.DomainLayer.UnitConverter.GetTime(modflow.ModelOption.Units.Time) + ")";
            this.cbiPet.Content = "PET.蒸散需求速率(" + Domain.DomainLayer.UnitConverter.GetLength(modflow.ModelOption.Units.Length) + "/" + Domain.DomainLayer.UnitConverter.GetTime(modflow.ModelOption.Units.Time) + ")";
            this.cbiExtdp.Content = "EXTDP.蒸散终止深度(" + Domain.DomainLayer.UnitConverter.GetLength(modflow.ModelOption.Units.Length) + ")";
            this.cbiVks.Content = "VKS.垂向水力传导系数(" + Domain.DomainLayer.UnitConverter.GetLength(modflow.ModelOption.Units.Length) + "/" + Domain.DomainLayer.UnitConverter.GetTime(modflow.ModelOption.Units.Time) + ")";
        }

        PCUzfCollection _uzfCollection;

        internal PCUzfCollection UzfCollection
        {
            get { return _uzfCollection; }
            set { _uzfCollection = value; }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //显示第一个应力期的时间点
            this.labStartTime.Text = "0.0";
            if (this._uzfCollection.TimePoint.Length <= 1)
                return;
            else
                this.labEndTime.Text = this._uzfCollection.TimePoint[1].ToString();

            this.cbIndex.Items.Clear();
            this.cbIndex.ItemsSource = this._uzfCollection.PeriodIndex;
            if (this._uzfCollection.PeriodIndex.Count > 0)
            {
                this.cbIndex.SelectedIndex = 0;
            }
            int index = this.cbIndex.SelectedIndex;
            IsShowViewEdit(index);

            //int index = this.cbIndex.SelectedIndex;
            //Binding bin = new Binding();         
            //bin.Path = new PropertyPath("IsUsePre["+index+"]");
            //this.chkUsePrevious.SetBinding(CheckBox.IsCheckedProperty, bin);
            //this.gridPre.DataContext = this._uzfCollection;

            ////当Use previous 选中时不可修改各属性值
            //Binding bindEabled = new Binding();
            //bindEabled.ElementName = "chkUsePrevious";
            //bindEabled.Path = new PropertyPath("IsChecked");
            //bindEabled.Converter = new NotConvertor();
            //this.btnReset.SetBinding(Button.IsEnabledProperty, bindEabled);

            // bindEabled = new Binding();
            //bindEabled.ElementName = "chkUsePrevious";
            //bindEabled.Path = new PropertyPath("IsChecked");
            //bindEabled.Converter = new NotConvertor();
            //this.txtMultiplier.SetBinding(TextBox.IsEnabledProperty, bindEabled);



            if (this.cboView.Items.Count > 0)
            {
                this.cboView.SelectedIndex = 0;
            }
            ShowData(this.cboView.SelectedIndex);

            this.DataContext = this._uzfCollection.Uzf;
        }

        private void cbIndex_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.cbIndex.SelectedIndex != -1)
            {
                int index = this.cbIndex.SelectedIndex;
                this.labStartTime.Text = this._uzfCollection.TimePoint[index].ToString();
                this.labEndTime.Text = this._uzfCollection.TimePoint[index + 1].ToString();

                Binding bin = new Binding();
                string str = "IsUsePre[" + index + "]";
                bin.Path = new PropertyPath(str);
                this.chkUsePrevious.SetBinding(CheckBox.IsCheckedProperty, bin);
                ShowData(this.cboView.SelectedIndex);
                this.gridPre.DataContext = this._uzfCollection;
                if (index == 0)
                {
                    this.chkUsePrevious.IsEnabled = false ;
                }
                else
                {
                    this.chkUsePrevious.IsEnabled = true ;
                }
            }
        }

        private void IsShowViewEdit(int index)
        {
            if (index == 0)
            {
                this.chkUsePrevious.IsEnabled = false;
            }
            else
            {
                this.chkUsePrevious.IsEnabled = true;
            }
            Binding bindEabled = new Binding();
            bindEabled.ElementName = "chkUsePrevious";
            bindEabled.Path = new PropertyPath("IsChecked");
            bindEabled.Converter = new NotConvertor();
            this.dtGrid.SetBinding(DataGrid.IsEnabledProperty, bindEabled);

             bindEabled = new Binding();
            bindEabled.ElementName = "chkUsePrevious";
            bindEabled.Path = new PropertyPath("IsChecked");
            bindEabled.Converter = new NotConvertor();
            this.btnReset.SetBinding(Button.IsEnabledProperty, bindEabled);

            bindEabled = new Binding();
            bindEabled.ElementName = "chkUsePrevious";
            bindEabled.Path = new PropertyPath("IsChecked");
            bindEabled.Converter = new NotConvertor();
            this.txtMultiplier.SetBinding(TextBox.IsEnabledProperty, bindEabled);

        }

        /// <summary>
        /// 重置数据
        /// </summary>
        /// <param name="data"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        private double[,] ResetDoubleOrIntData(double[,] data, double value)
        {
            for (int i = 0; i < data.GetLength(0); i++)
            {
                for (int j = 0; j < data.GetLength(1); j++)
                {
                    data[i, j] = value;
                }
            }
               return data ;
        }
        /// <summary>
        /// 重置数据
        /// </summary>
        /// <param name="data"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        private int[,] ResetDoubleOrIntData(int[,] data, int value)
        {
            for (int i = 0; i < data.GetLength(0); i++)
            {
                for (int j = 0; j < data.GetLength(1); j++)
                {
                    data[i, j] = value;
                }
            }
            return data;
        }

      
        /// <summary>
        /// 根据某种数据类型显示对应数据
        /// </summary>
        /// <param name="index"></param>
        private void ShowData(int index)
        {
            int PreIndex = this.cbIndex.SelectedIndex;
            if (PreIndex != -1)
            {
                Binding bindEabled = new Binding();
                switch (index)
                {
                    case 0: this.dtGrid.dgData.ItemsSource = PCGridArrayLayer<double>.GetBindable2DArray<double>(this._uzfCollection.Uzf.Periods[PreIndex].FINF);
                        this.dtGrid.SetDoubleData(this._uzfCollection.Uzf.Periods[PreIndex].FINF);

                        this.chkUsePrevious.IsEnabled = true;
                        this.cbIndex.IsEnabled = true;

                        IsShowViewEdit(PreIndex);


                        bindEabled.Path = new PropertyPath("Periods[" + PreIndex + "].Multiplier");
                        this.txtMultiplier.SetBinding(TextBox.TextProperty, bindEabled);
                        this.txtMultiplier.DataContext = this._uzfCollection.Uzf;
                        break;
                    case 1: dtGrid.dgData.ItemsSource = PCGridArrayLayer<double>.GetBindable2DArray<double>(this._uzfCollection.Uzf.Periods[PreIndex].PET);
                        this.dtGrid.SetDoubleData(this._uzfCollection.Uzf.Periods[PreIndex].PET);
                        this.chkUsePrevious.IsEnabled = true;
                        this.cbIndex.IsEnabled = true;
                        IsShowViewEdit(PreIndex);
                        bindEabled = new Binding();
                        bindEabled.Path = new PropertyPath("Periods[" + PreIndex + "].Multiplier");
                        this.txtMultiplier.SetBinding(TextBox.TextProperty, bindEabled);
                        this.txtMultiplier.DataContext = this._uzfCollection.Uzf;
                        break;
                    case 2: dtGrid.dgData.ItemsSource = PCGridArrayLayer<double>.GetBindable2DArray<double>(this._uzfCollection.Uzf.Periods[PreIndex].EXTDP);
                        this.dtGrid.SetDoubleData(this._uzfCollection.Uzf.Periods[PreIndex].EXTDP);
                        this.chkUsePrevious.IsEnabled = true;
                        this.cbIndex.IsEnabled = true;
                        IsShowViewEdit(PreIndex);
                        bindEabled = new Binding();
                        bindEabled.Path = new PropertyPath("Periods[" + PreIndex + "].Multiplier");
                        this.txtMultiplier.SetBinding(TextBox.TextProperty, bindEabled);
                        this.txtMultiplier.DataContext = this._uzfCollection.Uzf;
                        break;
                    case 3: dtGrid.dgData.ItemsSource = PCGridArrayLayer<int>.GetBindable2DArray<double>(this._uzfCollection.Uzf.Periods[PreIndex].EXTWC);
                        this.dtGrid.SetDoubleData(this._uzfCollection.Uzf.Periods[PreIndex].EXTWC);
                        this.chkUsePrevious.IsEnabled = true;
                        this.cbIndex.IsEnabled = true;
                        IsShowViewEdit(PreIndex);
                        bindEabled = new Binding();
                        bindEabled.Path = new PropertyPath("Periods[" + PreIndex + "].Multiplier");
                        this.txtMultiplier.SetBinding(TextBox.TextProperty, bindEabled);
                        this.txtMultiplier.DataContext = this._uzfCollection.Uzf;
                        break;
                    case 4: dtGrid.dgData.ItemsSource = PCGridArrayLayer<int>.GetBindable2DArray<int>(this._uzfCollection.Uzf.IUZFBND);
                        this.dtGrid.SetIntData(this._uzfCollection.Uzf.IUZFBND);
                        this.dtGrid.IsEnabled = true;
                        this.chkUsePrevious.IsEnabled = false;
                        this.cbIndex.IsEnabled = false;
                        this.btnReset.IsEnabled = true;
                        this.txtMultiplier.IsEnabled = true;
                        break;
                    case 5: dtGrid.dgData.ItemsSource = PCGridArrayLayer<int>.GetBindable2DArray<int>(this._uzfCollection.Uzf.IRUNBND);
                        this.dtGrid.SetIntData(this._uzfCollection.Uzf.IRUNBND);
                        this.dtGrid.IsEnabled = true;
                        this.chkUsePrevious.IsEnabled = false;
                        this.cbIndex.IsEnabled = false;
                        this.btnReset.IsEnabled = true;
                        this.txtMultiplier.IsEnabled = true;
                        break;
                    case 6: dtGrid.dgData.ItemsSource = PCGridArrayLayer<double>.GetBindable2DArray<double>(this._uzfCollection.Uzf.VKS);
                        this.dtGrid.SetDoubleData(this._uzfCollection.Uzf.VKS);
                        this.dtGrid.IsEnabled = true;
                        this.chkUsePrevious.IsEnabled = false;
                        this.cbIndex.IsEnabled = false;
                        this.btnReset.IsEnabled = true;
                        this.txtMultiplier.IsEnabled = true;
                    
                        break;
                    case 7: dtGrid.dgData.ItemsSource = PCGridArrayLayer<double>.GetBindable2DArray<double>(this._uzfCollection.Uzf.EPS);
                        this.dtGrid.SetDoubleData(this._uzfCollection.Uzf.EPS);
                        this.dtGrid.IsEnabled = true;
                        this.chkUsePrevious.IsEnabled = false;
                        this.cbIndex.IsEnabled = false;
                        this.btnReset.IsEnabled = true;
                        this.txtMultiplier.IsEnabled = true;
                        break;
                    case 8: dtGrid.dgData.ItemsSource = PCGridArrayLayer<double>.GetBindable2DArray<double>(this._uzfCollection.Uzf.THIS);
                        this.dtGrid.SetDoubleData(this._uzfCollection.Uzf.THIS);
                        this.dtGrid.IsEnabled = true;
                        this.chkUsePrevious.IsEnabled = false;
                        this.cbIndex.IsEnabled = false;
                        this.btnReset.IsEnabled = true;
                        this.txtMultiplier.IsEnabled = true;
                        break;
                    case 9: dtGrid.dgData.ItemsSource = PCGridArrayLayer<double>.GetBindable2DArray<double>(this._uzfCollection.Uzf.THTI);
                        this.dtGrid.SetDoubleData(this._uzfCollection.Uzf.THTI);
                        this.dtGrid.IsEnabled = true;
                        this.chkUsePrevious.IsEnabled = false;
                        this.cbIndex.IsEnabled = false;
                        this.btnReset.IsEnabled = true;
                        this.txtMultiplier.IsEnabled = true;
                        break;
                }
            }
        }

        private void cboView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ShowData(this.cboView.SelectedIndex);
        }

        private void chkUsePrevious_Click(object sender, RoutedEventArgs e)
        {
            int index = this.cbIndex.SelectedIndex;
            if (index != -1)
            {
                //this._uzfCollection.IsUsePre[index] = this.chkUsePrevious.IsChecked.Value;
                //if (this.chkUsePrevious.IsChecked.Value)
                //{
                //    this.btnReset.IsEnabled = false;
                //    this.txtMultiplier.IsEnabled = false;
                //    this.dtGrid.IsEnabled = false;
                //}
                //else
                //{
                //    this.btnReset.IsEnabled = true;
                //    this.txtMultiplier.IsEnabled = true;
                //    this.dtGrid.IsEnabled = true;
                //}
            }
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            this._uzfCollection.ConfirmToDataCenter();
            this.DialogResult = true;
            this.Close();
        }

        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            HA.ModFlow.Controls.UserControls.winResetValue winReset = new HA.ModFlow.Controls.UserControls.winResetValue(DataType.DOUBLE);
            winReset.Owner = this;
            if (winReset.ShowDialog().Value)
            {
                double value =Convert .ToDouble ( winReset.txtResetValue.Text);
                int index = this.cboView.SelectedIndex;
                if (index != -1)
                {
                    ResetData(index, value);
                    ShowData(index);
                }
            }
        }

        private void ResetData(int index ,double value)
        {
            int PreIndex = this.cbIndex.SelectedIndex;
            if (PreIndex != -1)
            {
                Binding bindEabled = new Binding();
                switch (index)
                {
                    case 0:
                        this._uzfCollection.Uzf.Periods[PreIndex].FINF = ResetDoubleOrIntData(this._uzfCollection.Uzf.Periods[PreIndex].FINF, value);
                        break;
                    case 1:
                        this._uzfCollection.Uzf.Periods[PreIndex].PET = ResetDoubleOrIntData(this._uzfCollection.Uzf.Periods[PreIndex].PET, value);                       
                        break;
                    case 2:
                        this._uzfCollection.Uzf.Periods[PreIndex].EXTDP = ResetDoubleOrIntData(this._uzfCollection.Uzf.Periods[PreIndex].EXTDP, value);                    
                        break;
                    case 3:
                        this._uzfCollection.Uzf.Periods[PreIndex].EXTWC = ResetDoubleOrIntData(this._uzfCollection.Uzf.Periods[PreIndex].EXTWC, value);                     
                        break;
                    case 4:
                        this._uzfCollection.Uzf.IUZFBND = ResetDoubleOrIntData(this._uzfCollection.Uzf.IUZFBND,Convert.ToInt32 ( value));                    
                        break;
                    case 5:
                        this._uzfCollection.Uzf.IRUNBND = ResetDoubleOrIntData(this._uzfCollection.Uzf.IRUNBND, Convert.ToInt32(value));                   
                        break;
                    case 6:
                        this._uzfCollection.Uzf.VKS = ResetDoubleOrIntData(this._uzfCollection.Uzf.VKS, value); 
                        break;
                    case 7:
                        this._uzfCollection.Uzf.EPS = ResetDoubleOrIntData(this._uzfCollection.Uzf.EPS, value);                     
                        break;
                    case 8:
                        this._uzfCollection.Uzf.THIS = ResetDoubleOrIntData(this._uzfCollection.Uzf.THIS, value);                      
                        break;
                    case 9:
                        this._uzfCollection.Uzf.THTI = ResetDoubleOrIntData(this._uzfCollection.Uzf.THTI, value);                      
                        break;
                }
            }
        }
       
    }
    public class ConvertToNUZTOP:IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int index = (int)value;
            int xx = 0;
            switch (index)
            {
                case 1:
                    xx = 0;
                    break;
                case 2:
                    xx = 1;
                    break;
                case 3:
                    xx = 2;
                    break;
            }
            return xx;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int index = (int)value;
            int xx = 0;
            switch (index)
            {
                case 0:
                    xx = 1;
                    break;
                case 1:
                    xx = 2;
                    break;
                case 2:
                    xx = 3;
                    break;
            }
            return xx;
        }
    }

    public class ConvertToIUZFOPT : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int index = (int)value;
            int xx = 0;
            switch (index)
            {
                case -2:
                    xx = 0;
                    break;
                case -1:
                    xx = 1;
                    break;
                case 1:
                    xx = 2;
                    break;
                case 2:
                    xx = 3;
                    break;
            }
            return xx;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int index = (int)value;
            int xx = 0;
            switch (index)
            {
                case 0:
                    xx = -2;
                    break;
                case 1:
                    xx = -1;
                    break;
                case 2:
                    xx = 1;
                    break;
                case 3:
                    xx = 2;
                    break;
            }
            return xx;
        }
    }

    public class ConvertToIsOnePre : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if ((int)value == 0)
            {
                return false  ;
            }
            else
            {
                return true  ;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }


}
